package com.dixon.worddayday.util;

import com.dixon.worddayday.model.Words;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author dixon
 * @version 1.0.0
 * @Description TODO
 * @createTime 2023年05月04日 21:35:00
 */
public class WordFormatUtil {
    final static Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5].+");
    public static void formatWordExampleSentence(Words words) {

        if(!StringUtils.isEmpty(words.getExampleSentence())) {
            StringBuilder sb = new StringBuilder();
            Arrays.stream(words.getExampleSentence().split("\\d\\.")).filter(e -> !StringUtils.isEmpty(e)).forEach(e -> {
                String chiness = null;
                String english = null;
                Matcher matcher = pattern.matcher(e);
                if(matcher.find()) {
                    chiness = matcher.group(0);
                    english = e.replace(chiness, "");
                }

                sb.append("<p><span>").append(english).append("</span><br/><span>").append(chiness).append("</span></p>");
            });

            words.setExampleSentence(sb.toString());
        }

    }
}
